package com.zhou;

import java.util.*;
import java.util.stream.Collectors;

/**
 * T17
 * <p>
 * 2 2 5
 * 1 1 1 2
 * 1 2 1 1
 * 1 3 2 2
 * 2 1 1 2
 * 2 3 5 5
 * <p>
 * 3
 * 4
 * 5
 * 3
 * 9
 *
 * @author zhouxianfeng
 * @date 2021-8-31
 */
public class T17 {
    /**
     * 输入第一行三个数N、M、P，分别表示有N个PM，M个程序员，P个idea。随后有P行，每行有4个数字，分别是PM序号、提出时间、优先等级和所需时间。全部数据范围 [1,
     * 3000]。
     *
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int p = sc.nextInt();
        sc.nextLine();
        List<Idea> list = new ArrayList<>();
        for (int i = 0; i < p; i++) {
            String[] s = sc.nextLine().split(" ");
            int[] ints =
                    Arrays.asList(s).stream().mapToInt(Integer::parseInt).toArray();
            list.add(new Idea(ints[0], ints[1], ints[2], ints[3], i));
        }
        resolve(list, m);
    }

    /**
     * 同时有M个程序员，每个程序员空闲的时候就会查看每个PM尚未执行并且最想完成的一个idea,
     * 然后从中挑选出所需时间最小的一个idea独立实现，如果所需时间相同则选择PM序号最小的。直到完成了idea
     * 才会重复上述操作。如果有多个同时处于空闲状态的程序员，那么他们会依次进行查看idea
     * 的操作。
     */
    public static void resolve(List<Idea> mm, int m) {
        int[] solveTimes = new int[mm.size()];

        List<Programer> programers = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            programers.add(new Programer(1));
        }
        int length = mm.size();
        List<Idea> ideas = sortIdea(mm);
        for (int j = 0; j < length; j++) {
            Programer programer = programers.get(0);
            Idea idea = null;
            do {
                for (Idea id : ideas) {
                    if (id.already == 0 && id.time <= programer.time) {
                        idea = id;
                    }
                    if (idea != null) {
                        break;
                    }
                }
                if (idea == null) {

                    programer.time += 1;
                }
            } while (idea == null);


            if (idea.time > programer.time) {
                programer.time = idea.time;
            }
            solveTimes[idea.index] = programer.time + idea.needTime;
            programer.time = programer.time + idea.needTime;
            idea.already = 3;
            ideas.remove(idea);
            programers.sort(new Comparator<Programer>() {
                @Override
                public int compare(Programer o1, Programer o2) {
                    return o1.time - o2.time;
                }
            });
        }

        for (int i = 0; i < solveTimes.length; i++) {
            System.out.println(solveTimes[i]);
        }

    }


    public static List<Idea> sortIdea(List<Idea> mm) {

        List<Idea> collect =
                mm.stream().sorted(new Comparator<Idea>() {
                    @Override
                    public int compare(Idea o1, Idea o2) {
                        int result = 0;
                        int levelGap = o1.level - o2.level;
                        result = levelGap;
                        if (levelGap == 0) {
                            int needTimeGap = o1.needTime - o2.needTime;
                            result = needTimeGap;
                            if (needTimeGap == 0) {
                                int timegap = o1.time - o2.time;
                                result = timegap;
                                if (timegap == 0) {
                                    int pmIdGap = o1.pmId - o2.pmId;
                                    result = pmIdGap;
                                }

                            }
                        }
                        return result;
                    }
                }).collect(Collectors.toList());
        return collect;

    }

    static class Idea {
        int pmId;
        int time;
        int level;
        int needTime;
        int index;

        int already;

        Idea(int a1, int a2, int a3, int a4, int a5) {
            pmId = a1;
            time = a2;
            level = a3;
            needTime = a4;
            index = a5;
        }
    }

    static class Programer {
        int time;

        Programer(int time) {
            this.time = time;
        }
    }

}
